//
//  SecurityGroupDelegate.m
//  cTypica
//
//  Created by David Kavanagh on 11/21/08.
//  Copyright 2008 directThought. All rights reserved.
//

#import "SecurityGroupDelegate.h"

@implementation SecurityGroupDelegate
@synthesize groups;

+ (id)delegate {
	SecurityGroupDelegate *ret = [self alloc];
	return ret;
}

// XML parser delegate methods
- (void)parser: (NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
	if ([elementName isEqualToString:@"securityGroupInfo"]) {
		self.groups = [[NSMutableArray alloc] initWithCapacity:1];
		inPerms = NO;
	}
	else if ([elementName isEqualToString:@"item"]) {
		if (!inPerms && !inGroups && !inRanges) {
			item = [GroupInfo alloc];
		}
		else if (inPerms && !inGroups && !inRanges) {
			ipPerms = [IpPermission alloc];
		}
	}
	else if ([elementName isEqualToString:@"ipPermissions"]) {
		inPerms = YES;
		item.perms = [[NSMutableArray alloc] initWithCapacity:1];
	}
	else if ([elementName isEqualToString:@"ipRanges"]) {
		inRanges = YES;
		ipPerms.cidrIps = [[NSMutableArray alloc] initWithCapacity:1];
	}
	else if ([elementName isEqualToString:@"groups"]) {
		inGroups = YES;
		ipPerms.uidGroupPairs = [[NSMutableArray alloc] initWithCapacity:1];
	}
}


- (void)parser: (NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	NSString *cleanString = [currentStringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	[currentStringValue setString:@""];
	if ([elementName isEqualToString:@"securityGroupInfo"]) {
	}
	else if ([elementName isEqualToString:@"item"]) {
		if (!inPerms && !inGroups && !inRanges) {
			[self.groups addObject:item];
			[item release];
			item = nil;
		}
		else if (inPerms && !inGroups && !inRanges) {
			[item.perms addObject:ipPerms];
			[ipPerms release];
			ipPerms = nil;
		}
	}
	else if ([elementName isEqualToString:@"ownerId"]) {
		item.owner = cleanString;
	}
	else if ([elementName isEqualToString:@"groupName"]) {
		if (!inPerms) {
			item.name = cleanString;
		}
		else {
			[ipPerms.uidGroupPairs addObject:[NSArray arrayWithObjects:userId, cleanString, nil]];
			//[cleanString release];
		}
	}
	else if ([elementName isEqualToString:@"groupDescription"]) {
		item.desc = cleanString;
	}
	else if ([elementName isEqualToString:@"ipPermissions"]) {
		inPerms = NO;
	}
	else if ([elementName isEqualToString:@"groups"]) {
		inGroups = NO;
	}
	else if ([elementName isEqualToString:@"ipRanges"]) {
		inRanges = NO;
	}
	else if ([elementName isEqualToString:@"ipProtocol"]) {
		ipPerms.protocol = cleanString;
	}
	else if ([elementName isEqualToString:@"fromPort"]) {
		ipPerms.fromPort = [cleanString intValue];
		//[cleanString release];
	}
	else if ([elementName isEqualToString:@"toPort"]) {
		ipPerms.toPort = [cleanString intValue];
		//[cleanString release];
	}
	else if ([elementName isEqualToString:@"cidrIp"]) {
		[ipPerms.cidrIps addObject:cleanString];
	}
	else if ([elementName isEqualToString:@"userId"]) {
		userId = cleanString;
	}
}
@end
